package com.capgemini.scaffold.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.capgemini.scaffold.common.Admin;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.model.request.*;
import com.capgemini.scaffold.model.vo.UserVo;
import com.capgemini.scaffold.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.capgemini.scaffold.model.User;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


/**
 * 用户接口
 *
 * @author llf,wc
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseBean<Long> UserRegister(@RequestBody UserRegisterReq userRegisterReq) {
        if (userRegisterReq == null) {
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR);
        }
        String userAccount = userRegisterReq.getUserAccount();
        String userPassword = userRegisterReq.getUserPassword();
        String checkPassword = userRegisterReq.getCheckPassword();
        String userName = userRegisterReq.getUserName();
        String positionId = userRegisterReq.getPositionId();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, userName)) {
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"注册参数为空");
        }
        return ResponseBean.success(userService.userRegister(userAccount, userPassword, checkPassword, userName, positionId));
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResponseBean<User> UserLogin(@RequestBody UserLoginReq userLoginReq, HttpServletRequest request) {
        if (userLoginReq == null) {
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR);
        }
        String userAccount = userLoginReq.getUserAccount();
        String userPassword = userLoginReq.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"注册参数为空");
        }
        return ResponseBean.success(userService.userLogin(userAccount, userPassword, request));
    }

    @ApiOperation("获取leader列表")
    @PostMapping("/getLeader")
    public ResponseBean<User> GetLeader() {
        return ResponseBean.success(userService.getLeader());
    }


    @ApiOperation("获取不在项目的雇员列表")
    @PostMapping("/getEmployee")
    public ResponseBean<User> GetEmployee() {
        return ResponseBean.success(userService.getEmployee());
    }

//    @ApiOperation("新增用户")
//    @PostMapping("/add")
//    public ResponseBean<User> Add(@RequestBody User user) {
//        return ResponseBean.success(userService.add(user));
//    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/delete/{id}/{isRole}")
    public ResponseBean<Integer> DeleteById(@PathVariable("id") Integer id, @PathVariable Integer isRole) {
        if (!Admin.isAdmin(isRole)) {
            throw new BusinessException(ExceptionEnum.NO_AUTH, "你没有权限");
        }
        if (id <= 0) {
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR, "该用户不存在");
        }
        boolean b = userService.delete(id);
        return ResponseBean.success(b);
    }

    @ApiOperation("管理员更新用户信息")
    @PutMapping("/update")
    public ResponseBean<Integer> Update(@RequestBody AdminUpdateReq adminUpdateReq) {
        return ResponseBean.success(userService.update(adminUpdateReq));
    }

    @ApiOperation("普通用户更新用户信息")
    @PutMapping("/userUpdate")
    public ResponseBean<Integer> UseerUpdate(@RequestBody UserUpdateReq userUpdateReq){
        return ResponseBean.success(userService.userUpdate(userUpdateReq));
    }

    @ApiOperation("根据id获取用户信息")
    @GetMapping("/get/{id}")
    public ResponseBean<User> Get(@PathVariable("id") Integer id) {
        return ResponseBean.success(userService.get(id));
    }

    @ApiOperation("用户查询接口")
    @PostMapping("/queryUser")
    public ResponseBean<List<UserVo>> QueryUser(@RequestBody UserReq req) {
        return ResponseBean.success(userService.queryUser(req));
    }

    @ApiOperation("根据id查询")
    @PostMapping("/queryUserById")
    public ResponseBean<User> QueryUserById(Integer userId){
        return ResponseBean.success(userService.queryUserById(userId));
    }

//    @ApiOperation("分页查询用户信息")
//    @PostMapping("/queryUserPage")
//    public ResponseBean<IPage<UserVo>> QueryUserPage(@RequestBody UserReq req) {
//        return ResponseBean.success(userService.queryUserPage(req));
//    }

    @ApiOperation("更换头像")
    @PostMapping( "/saveImg")
    public String uploadFile(@RequestParam("file") MultipartFile file,Integer userId) throws IOException {
        return userService.saveImg(file,userId);
    }
}